package com.gears_software.bbnetwork.activitys;

import java.io.InputStream;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.WindowFeature;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Window;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.utlis.CommonUtils;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

/**
 * 报单提交成功分享页
 * 
 * @author zhouyong
 * 
 */
@EActivity(R.layout.activity_bdsucceed)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class BaoDanSucceedActivity extends Activity {

	@Bean
	ToastUI toast;
	UMSocialService mController;
	UMQQSsoHandler qqSsoHandler;
	UMWXHandler wxCircleHandler;
	UMWXHandler wxHandler;
	@AfterViews
	void onCreate() {
		// 首先在您的Activity中添加如下成员变量
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		// 设置分享内容
		mController.setShareContent("http://www.bibefc.com/mobile/share.html");
		InputStream input = getResources()
				.openRawResource(R.drawable.name_logo);
		Bitmap bitmap = BitmapFactory.decodeStream(input);
		// 设置分享图片, 参数2为图片的url地址
		mController.setShareMedia(new UMImage(this, bitmap));
		String appID = "wx726a273d06a8ebf0";
		String appSecret = "5e8de194084ce5195c95247fabacd188";

		// 添加微信平台
		 wxHandler = new UMWXHandler(this, appID, appSecret);
		wxHandler.addToSocialSDK();

		// 添加微信朋友圈
		wxCircleHandler = new UMWXHandler(this, appID, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		// 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		qqSsoHandler = new UMQQSsoHandler(this, "1104759317",
				"awAYUqcXhajIW4K7");
		qqSsoHandler.addToSocialSDK();

	}

	// 朋友圈分享按钮
	@Click
	void layout_pengyouquan() {
		shareFriend();
	}

	// QQ分享按钮
	@Click
	void layout_QQ() {
		shareQQ();
	}

	// 微信
	@Click
	void relative_weixin() {
		weixinFriend();
	}

	// 短信分享
	@Click
	void relative_sms() {
		Intent intent = new Intent(Intent.ACTION_SENDTO);
		intent.setData(Uri.parse("smsto:"));
		intent.putExtra("sms_body",
				"比比投标保函，安全，便捷，高效；在线办理：http://www.bibefc.com；客服电话：4000000388");
		startActivity(intent);

	}

	private void weixinFriend() {
		wxHandler.setTitle("比翼联盟分享");
		wxHandler.setTargetUrl("http://www.bibefc.com/mobile/share.html");
		mController.postShare(this, SHARE_MEDIA.WEIXIN, new SnsPostListener() {

			@Override
			public void onStart() {
				// toast.ShowToast("开始分享");

			}

			@Override
			public void onComplete(SHARE_MEDIA arg0, int arg1,
					SocializeEntity arg2) {
				if (arg1 == 200) {
//					toast.ShowToast("分享成功！");
				} else {
					String eMsg = "";
					if (arg1 == -101) {
						eMsg = "没有授权";
					}
					toast.ShowToast("分享失败或已取消[" + arg1 + "]" + eMsg);
				}
			}
		});
	}

	// 朋友圈分享
	private void shareFriend() {
		wxCircleHandler.setTitle("比翼联盟分享");
		wxCircleHandler.setTargetUrl("http://www.bibefc.com/mobile/share.html");
		mController.postShare(this, SHARE_MEDIA.WEIXIN_CIRCLE,
				new SnsPostListener() {

					@Override
					public void onStart() {
						// toast.ShowToast("开始分享");

					}

					@Override
					public void onComplete(SHARE_MEDIA arg0, int arg1,
							SocializeEntity arg2) {
						if (arg1 == 200) {
							toast.ShowToast("分享成功！");
						} else {
							String eMsg = "";
							if (arg1 == -101) {
								eMsg = "没有授权";
							}
							toast.ShowToast("分享失败或已取消[" + arg1 + "]" + eMsg);
						}
					}
				});
	}

	private void shareQQ() {
		boolean isqq = CommonUtils.isApkInstalled(this, "com.tencent.mobileqq");
		if (!isqq) {
			toast.ShowToast("您还没有装QQ软件");
			return;
		}
		qqSsoHandler.setTitle("比翼联盟分享");
		qqSsoHandler.setTargetUrl("http://www.bibefc.com/mobile/share.html");
		mController.postShare(this, SHARE_MEDIA.QQ, new SnsPostListener() {

			@Override
			public void onStart() {
				// toast.ShowToast("开始分享");
			}

			@Override
			public void onComplete(SHARE_MEDIA arg0, int arg1,
					SocializeEntity arg2) {
				if (arg1 == 200) {
//					toast.ShowToast("分享成功！");
				} else {
					String eMsg = "";
					if (arg1 == -101) {
						eMsg = "没有授权";
					}
					toast.ShowToast("分享失败或已取消[" + arg1 + "]" + eMsg);
				}
			}
		});
	}

	// 回退	
	@Click
	void iamge_back() {
		finish();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AVAnalytics.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}
}